package model.workersWithData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.apache.log4j.Logger;

import api.entitites.IMaster;
import api.entitites.IOrder;
import api.messages.Messager;

public class WorkerWithMaster {
	
	private static ArrayList<String> arrayMessages= new ArrayList<String>();
	private static final Logger log = Logger.getLogger(WorkerWithMaster.class);

	public static ArrayList<String> getOrdersByMaster(String nameMaster,
			ArrayList<IMaster> masList) {
		arrayMessages.clear();
		try {
			IOrder order = null;
			for (IMaster m : masList) {
				if (m.getNameMaster().equals(nameMaster)) {
					order = m.getOrder();
					
				}
			}
			if (order != null) {
				arrayMessages.add(Messager.MASTER+nameMaster+Messager.SERVICE+order.getNumberOrder());
			} else {
				arrayMessages.add(Messager.MAS_NOT_EXISTS);
			}
			
		} catch (NullPointerException n) {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_MASTER);

		}
		return arrayMessages;
	}

	public static ArrayList<String> SortMaster(ArrayList<IMaster> masterList,
			Comparator sortBy) { 
		arrayMessages.clear();
		if(!masterList.isEmpty()){
			Collections.sort(masterList, sortBy);
			 
			for (IMaster master : masterList) {
				if (master.getEmploymentMaster()) {
					arrayMessages.add(Messager.MASTER+master.getNameMaster()+Messager.BUSY_MASTER);
				} else {
					arrayMessages.add(Messager.MASTER+master.getNameMaster()+Messager.FREE_MASTER);
				}
			}
							
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_MASTER);
		}
		return arrayMessages;
	}

	public static ArrayList<String> addMaster(ArrayList<IMaster> masterList, IMaster master) { 
		try {
			arrayMessages.clear();
			int i = 0;
			for (IMaster m : masterList) {
				if (m.getNameMaster().equals(master.getNameMaster())) {
					i++;
					arrayMessages.add(Messager.MAS_EXISTS);
				}
			}
			if (i == 0) {
				masterList.add(master);
				arrayMessages.add(Messager.MASTER_ADD);
			}
		} catch (NullPointerException n) {
			log.warn(Messager.EMPTY_COLLEC);

		}
		return arrayMessages;

	}

	public static ArrayList<String> deleteMaster(ArrayList<IMaster> masterList,
			String masterName) {
		arrayMessages.clear();
		try {
			ArrayList<IMaster> temp = new ArrayList<IMaster>();
			for (IMaster mas : masterList) {
				if (!mas.getNameMaster().equals(masterName)) {
					temp.add(mas);
				}
			}
			if (temp.size() == masterList.size()) {
				arrayMessages.add(Messager.MAS_NOT_EXISTS);
			} else {
				masterList.clear();
				masterList.addAll(temp);
				arrayMessages.add(Messager.MASTER_DELETE);
				
			}
		} catch (NullPointerException n) {
			log.warn(Messager.EMPTY_COLLEC);

		}
		return arrayMessages;
	}

}
